package com.win.oauth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fyl.controller.BaseController;
import com.fyl.core.advance.constant.enums.CommonResponseEnum;
import com.fyl.core.response.RS;
import com.win.oauth.dto.UserDTO;
import com.win.oauth.entity.*;
import com.win.oauth.service.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author youlian.fei
 * @since 2020-03-14
 */
@RestController
@RequestMapping("/oauth/user")
public class UserController extends BaseController<IUserService, User> {
    @Autowired
    IModuleService moduleService;

    @Autowired
    IFunService funService;

    @Autowired
    IPermissionService permissionService;

    @Autowired
    IRoleService roleService;

    @Autowired
    IUserRoleRelService userRoleRelService;

    @Autowired
    IRolePermissionRelService rolePermissionRelService;

    @GetMapping("/getRoles")
    public RS<List<Role>> getRoles() {
        List<Role> roles = roleService.list();
        return RS.genSuccess(roles);
    }

    @Override
    public RS<User> get(@RequestParam Long id) {
        User user = baseService.getById(id);
        UserRoleRel userRoleRel = new UserRoleRel();
        userRoleRel.setUserId(id);
        List<UserRoleRel> list = userRoleRelService.list(Wrappers.query(userRoleRel));
        List<Long> roleIds = list.stream().map(UserRoleRel::getRoleId).collect(Collectors.toList());
        UserDTO resp = new UserDTO();
        BeanUtils.copyProperties(user, resp);
        resp.setRoleIds(roleIds);
        return RS.genSuccess(resp);
    }

    @PostMapping("/save")
    public RS<Void> save(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);

        if (null==user.getId()){
            Integer count = this.baseService.lambdaQuery().eq(User::getName, user.getName()).count();
            if (count > 0) {
                return RS.genFail("用户名重复");
            }

            String encryptedPwd = DigestUtils.md5Hex(user.getName() + user.getPassword());
            user.setPassword(encryptedPwd);
        }

        baseService.saveOrUpdate(user);
        List<UserRoleRel> userRoleRels = userDTO.getRoleIds().stream().map(roleId -> {
            UserRoleRel rel = new UserRoleRel();
            rel.setUserId(user.getId());
            rel.setRoleId(roleId);
            return rel;
        }).collect(Collectors.toList());

        UserRoleRel userRoleRel = new UserRoleRel();
        userRoleRel.setUserId(user.getId());
        userRoleRelService.remove(Wrappers.query(userRoleRel));
        if (!userRoleRels.isEmpty()) {
            userRoleRelService.saveBatch(userRoleRels);
        }
        return RS.genSuccess();
    }

    @PostMapping({"/delete"})
    public RS<Void> delete(@RequestBody Collection<Long> idList) {
        baseService.removeByIds(idList);
        userRoleRelService.remove(new LambdaQueryWrapper<UserRoleRel>().in(UserRoleRel::getUserId, idList));
        return RS.genSuccess();
    }
}
